package chat;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class ChatClient {
    private JFrame frame;
    private JTextArea chatArea;
    private JTextField inputField;
    private PrintWriter out;
    private String clientName;

    public ChatClient() {
        // 获取用户名
        clientName = JOptionPane.showInputDialog("请输入你的用户名:");
        if (clientName == null || clientName.trim().isEmpty()) {
            clientName = "匿名用户";
        }

        // 创建GUI
        frame = new JFrame("局域网聊天 - " + clientName);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 400);

        chatArea = new JTextArea();
        chatArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(chatArea);

        inputField = new JTextField();
        inputField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sendMessage();
            }
        });

        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
        frame.getContentPane().add(inputField, BorderLayout.SOUTH);
        frame.setVisible(true);

        // 连接服务器
        connectToServer();
    }

    private void connectToServer() {
        try {
            Socket socket = new Socket("172.16.74.60", 12345); // 如果是局域网，可以改为服务器IP
            out = new PrintWriter(socket.getOutputStream(), true);

            // 启动线程接收服务器消息
            new Thread(new ServerListener(socket)).start();

            // 发送欢迎消息
            out.println(clientName + " 加入了聊天室");
        } catch (IOException e) {
            chatArea.append("无法连接到服务器: " + e.getMessage() + "\n");
        }
    }

    private void sendMessage() {
        String message = inputField.getText();
        if (!message.trim().isEmpty() && out != null) {
            out.println(clientName + ": " + message);
            inputField.setText("");
        }
    }

    private class ServerListener implements Runnable {
        private Socket socket;
        private BufferedReader in;

        public ServerListener(Socket socket) {
            this.socket = socket;
        }

        public void run() {
            try {
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String message;
                while ((message = in.readLine()) != null) {
                    final String msg = message;
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            chatArea.append(msg + "\n");
                        }
                    });
                }
            } catch (IOException e) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        chatArea.append("与服务器的连接已断开\n");
                    }
                });
            } finally {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        new ChatClient();
    }
}
